package order_server

import (
	"context"
	"google.golang.org/grpc"
	"jihe.common/protos/order_server/proto"
	"strings"
	"time"
)

var once = false
var conns []proto.OrderClient

func GetClient() []proto.OrderClient {
	if !once {
		for i := 0; i < len(conf.Get().Grpc.OrderServers); i++ {
			grpcConn, err := grpc.Dial(conf.Get().Grpc.OrderServers[i], grpc.WithInsecure())
			if err != nil {
				return conns
			}
			conns = append(conns, proto.NewOrderClient(grpcConn))
		}
		once = true
	}
	return conns
}

func OrderCreate(c context.Context, in *proto.OrderOrder) (out *proto.OrderOrder, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderCreate(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderGetListByDuid(c context.Context, in *proto.OrderGetListByDuidReq) (out *proto.OrderGetListByDuidRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderGetListByDuid(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderGetDetailById(c context.Context, in *proto.OrderOrder) (out *proto.OrderOrder, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderGetDetailById(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderCreateProductionMaterial(c context.Context, in *proto.OrderProductionMaterial) (out *proto.OrderCreateProductionMaterialRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderCreateProductionMaterial(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderUpdateProductionMaterialStatus(c context.Context, in *proto.OrderUpdateProductionMaterialStatusReq) (out *proto.OrderNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderUpdateProductionMaterialStatus(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderUpdatePayComplete(c context.Context, in *proto.OrderOrder) (out *proto.OrderNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderUpdatePayComplete(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderGetList(c context.Context, in *proto.OrderGetListReq) (out *proto.OrderGetListRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderGetList(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderGetStatusByIds(c context.Context, in *proto.OrderGetStatusByIdsReq) (out *proto.OrderStatuses, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderGetStatusByIds(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderUpdateComposingStatus(c context.Context, in *proto.OrderOrder) (out *proto.OrderNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderUpdateComposingStatus(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderUpdateComposingZip(c context.Context, in *proto.OrderOrder) (out *proto.OrderNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderUpdateComposingZip(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderProductionStatusMove(c context.Context, in *proto.OrderOrder) (out *proto.OrderNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderProductionStatusMove(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderLogisticsStatusMove(c context.Context, in *proto.OrderOrder) (out *proto.OrderNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderLogisticsStatusMove(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderCreateWithdrawRecord(c context.Context, in *proto.WithdrawRecord) (out *proto.OrderNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderCreateWithdrawRecord(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderGetLastWithdrawRecord(c context.Context, in *proto.WithdrawRecord) (out *proto.WithdrawRecord, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderGetLastWithdrawRecord(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderUpdateWithdrawRecordStatus(c context.Context, in *proto.WithdrawRecord) (out *proto.OrderNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderUpdateWithdrawRecordStatus(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderGetWithdrawRecordList(c context.Context, in *proto.OrderGetWithdrawRecordListReq) (out *proto.OrderGetWithdrawRecordListRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderGetWithdrawRecordList(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderWithdrawStatusMove(c context.Context, in *proto.WithdrawRecord) (out *proto.OrderNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderWithdrawStatusMove(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func ComposingByOrderId(c context.Context, in *proto.ComposingByOrderIdReq) (out *proto.ComposingByOrderIdRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].ComposingByOrderId(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func OrderGetProductionMaterialById(c context.Context, in *proto.OrderGetProductionMaterialByIdReq) (out *proto.OrderProductionMaterials, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].OrderGetProductionMaterialById(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}
